package com.moody.hibernate.dao;

import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.moody.hibernate.domain.CustAddress;

/**
 * A data access object (DAO) providing persistence and search support for
 * CustAddress entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * Copyright © Moody analytics
 * @see com.moody.hibernate.domain.CustAddress
 * @author MyEclipse Persistence Tools
 */

public class CustAddressDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory
			.getLogger(CustAddressDAO.class);
	// property constants
	public static final String CUST_ID = "custId";
	public static final String ADDRESS_TYPE = "addressType";
	public static final String COUNTRY_CODE = "countryCode";
	public static final String PROVINCE_CODE = "provinceCode";
	public static final String CITY_CODE = "cityCode";
	public static final String ADDRESS_DESC = "addressDesc";
	public static final String POST_CODE = "postCode";
	public static final String TEL = "tel";
	public static final String FAX = "fax";
	public static final String WEB_SITE_URL = "webSiteUrl";

	protected void initDao() {
		// do nothing
	}

	public void save(CustAddress transientInstance) {
		log.debug("saving CustAddress instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	
	public void update(CustAddress transientInstance) {
		log.debug("saving CustAddress instance");
		try {
			getHibernateTemplate().update(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	

	public void delete(CustAddress persistentInstance) {
		log.debug("deleting CustAddress instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public CustAddress findById(java.lang.String id) {
		log.debug("getting CustAddress instance with id: " + id);
		try {
			CustAddress instance = (CustAddress) getHibernateTemplate().get(
					"com.moody.hibernate.domain.CustAddress", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(CustAddress instance) {
		log.debug("finding CustAddress instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding CustAddress instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from CustAddress as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByCustId(Object custId) {
		return findByProperty(CUST_ID, custId);
	}

	public List findByAddressType(Object addressType) {
		return findByProperty(ADDRESS_TYPE, addressType);
	}

	public List findByCountryCode(Object countryCode) {
		return findByProperty(COUNTRY_CODE, countryCode);
	}

	public List findByProvinceCode(Object provinceCode) {
		return findByProperty(PROVINCE_CODE, provinceCode);
	}

	public List findByCityCode(Object cityCode) {
		return findByProperty(CITY_CODE, cityCode);
	}

	public List findByAddressDesc(Object addressDesc) {
		return findByProperty(ADDRESS_DESC, addressDesc);
	}

	public List findByPostCode(Object postCode) {
		return findByProperty(POST_CODE, postCode);
	}

	public List findByTel(Object tel) {
		return findByProperty(TEL, tel);
	}

	public List findByFax(Object fax) {
		return findByProperty(FAX, fax);
	}

	public List findByWebSiteUrl(Object webSiteUrl) {
		return findByProperty(WEB_SITE_URL, webSiteUrl);
	}

	public List findAll() {
		log.debug("finding all CustAddress instances");
		try {
			String queryString = "from CustAddress";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public CustAddress merge(CustAddress detachedInstance) {
		log.debug("merging CustAddress instance");
		try {
			CustAddress result = (CustAddress) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(CustAddress instance) {
		log.debug("attaching dirty CustAddress instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(CustAddress instance) {
		log.debug("attaching clean CustAddress instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static CustAddressDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (CustAddressDAO) ctx.getBean("CustAddressDAO");
	}

	/*
	 * 以下函数由自己封装
	 */
	public void deleteByCustId(String custId) {
		String hql = "delete from CustAddress where custId = ?";
		Session session = getHibernateTemplate().getSessionFactory().openSession();
		Query query = session.createQuery(hql);
		query.setParameter("custId", custId);
		if(query.executeUpdate() > 0)
			log.info("删除成功");
		else
			log.info("删除0个元素");
		session.close();

	}
	
	public void deleteById(String serialNo){
		String hql = "delete from CustAddress where serialNo = ?";
		Session session = getHibernateTemplate().getSessionFactory().openSession();
		Query query = session.createQuery(hql);
		query.setParameter(0, serialNo);
		if(query.executeUpdate() > 0)
			log.info("删除成功");
		else
			log.info("删除0个元素");
		session.close();

	}
	
	
	//批量插入
	@SuppressWarnings("unchecked")
	public void insert(Collection custAddress){
		getHibernateTemplate().saveOrUpdateAll(custAddress);
	}
}